//
//  ApiProvider.swift
//  Lease_IOS
//
//  Created by Mr.wang on 2018/12/20.
//  Copyright © 2018 Mr.wang. All rights reserved.
//

import Moya
import RxSwift
import KeychainSwift

class ApiProvider<Target>: MoyaProvider<Target> where Target: TargetType {
    
    init(plugins: [PluginType]) {
        var p = plugins
        p.append(RequestLogPlugin())
        p.append(ApiEncryptionPlugin())
        super.init(plugins: p)
    }
}

let provider = ApiProvider<MultiTarget>(plugins: [RequestErrorPlugin()])
let quietProvider = ApiProvider<MultiTarget>(plugins: [])

extension TargetType {
    
    func quietRequest() -> Observable<Response> {
        return quietProvider.rx.request(.target(self)).asObservable().map{ $0.decode() }
    }
    
    func request() -> Observable<Response> {
        return provider.rx.request(.target(self)).asObservable().map{ $0.decode() }
    }
    
    func loadCache() -> Observable<Response> {
        return provider.rx.loadCache(.target(self)).map{ $0.decode() }
    }
    
}
